package com.yunquan.ohana.ui.calendar.profiles.list

import android.app.Application
import android.os.Bundle
import androidx.core.graphics.toColorInt
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.google.gson.Gson
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.bus.event.SingleLiveEvent
import com.yunquan.ohana.base.ui.BaseViewModel
import com.yunquan.ohana.entity.RoleEntity
import com.yunquan.ohana.http.BaseHttp
import com.yunquan.ohana.http.observer.BaseObserver
import com.yunquan.ohana.http.transformer.CommonTransformer
import com.yunquan.ohana.ui.calendar.profiles.add.AddProfilesActivity
import io.reactivex.rxjava3.disposables.Disposable
import me.tatarka.bindingcollectionadapter2.ItemBinding
import okhttp3.RequestBody.Companion.toRequestBody

class ProfilesListViewModel(application: Application) : BaseViewModel(application) {

    val ui = SingleLiveEvent<Int>()

    var mDeviceId: String? = null
    var userType = false


    var data: RoleEntity? = null
    val prefix = ObservableField<String>()
    val name = ObservableField<String>()
    val account = ObservableField<String>()
    val color = ObservableField<Int>()
    val showAdd = ObservableField(false)

    val items = ObservableArrayList<ProfilesListItemViewModel>()
    val itemBinding = ItemBinding.of<ProfilesListItemViewModel>(
        BR.profilesListItemViewModel,
        R.layout.item_profile_list,
    )

    val onAddClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            val bundle = Bundle()
            bundle.putString("deviceId", mDeviceId)
            bundle.putBoolean("userType", userType)
            startActivityResult(AddProfilesActivity::class.java, bundle)
        }
    })

    val onProfileClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            if (data != null) {
                onEditProfiles(data!!)
            }
        }
    })

    fun onEditProfiles(data: RoleEntity) {
        val bundle = Bundle()
        bundle.putString("deviceId", mDeviceId)
        bundle.putBoolean("userType", userType)
        bundle.putString("data", Gson().toJson(data))
        startActivityResult(AddProfilesActivity::class.java, bundle)
    }

    fun getProfilesList() {
        val map = mutableMapOf<String, Any>()
        map["deviceId"] = mDeviceId!!
        val body = Gson().toJson(map).toRequestBody(BaseHttp.MEDIA_TYPE_JSON)
        BaseHttp.api.getDeviceRoleListById(body)
            .compose(CommonTransformer())
            .subscribe(object : BaseObserver<List<RoleEntity>>() {
                override fun onSubscribe(d: Disposable) {
                    super.onSubscribe(d)
                    showLoading()
                }

                override fun onSuccess(result: List<RoleEntity>?) {
                    super.onSuccess(result)
                    items.clear()
                    if (result != null) {
                        for (i in result.indices) {
                            if (i == 0) {
                                data = result[i]
                                prefix.set(data?.name?.first().toString())
                                name.set(data?.name)
                                account.set(data?.account)
                                color.set(data?.getColorData()?.inner?.toColorInt())
                            } else {
                                items.add(ProfilesListItemViewModel(this@ProfilesListViewModel, result[i]))
                            }
                        }
                    }
                }

                override fun onComplete() {
                    super.onComplete()
                    hideLoading()
                }
            })
    }
}